﻿using Common;
using System;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Linq;
using System.Web.UI.WebControls;
using System.Xml.Serialization;

namespace CarPCBATest.Procedure
{
    public enum DOFunction
    {
        [Description("LDM格栅控制供电")]
        DO1=5,

        [Description("中配DRL-L白色")]
        DO2,

        [Description("中配DRL-L黄色")]
        DO3,

        [Description("中配DRL-R白色")]
        DO4,

        [Description("中配DRL-R黄色")]
        DO5,
        [Description("中配LB")]
        DO6,
        [Description("中配HB")]
        DO7,
        [Description("中配ULL")]
        DO8,
        [Description("中配ULR")]
        DO9,
        [Description("低配大灯L白色")]
        DO10,
        [Description("低配大灯L黄色")]
        DO11,
        [Description("低配大灯R白色")]
        DO12=17,
        [Description("低配大灯R黄色")]
        DO13,
    
    }
    [Serializable]
    public class DOConfig : NotifyBase
    {
        [XmlAttribute("输出描述")]
        public DOFunction DOFunction { get; set; }

        private bool _isOn;
        [XmlAttribute("开启状态")]
        public bool IsOn
        {
            get { return _isOn; }
            set
            {
                _isOn = value;
                this.DoNotify();
            }
        }

        public DOConfig(DOFunction function, bool status)
        {
            DOFunction = function;
            IsOn = status;
        }
        public DOConfig()
        {
            
        }
    }

    [XmlRoot("控制板配置")]
    [Serializable]
    public class AllPcbConfig : NotifyBase
    {
        [XmlElement("OK数量")]
        public int OKCount { get; set; }
        [XmlElement("NG数量")]
        public int NGCount { get; set; }
        [XmlElement("标题")]
        public string Title { get; set; }

        [XmlElement("DM3058E串口")]
        public string DM3025EPort { get; set; }

        [XmlElement("IV3003T串口")]
        public string IV3003TPort { get; set; }

        //private string _webServiceUrl;

        //[XmlElement("WebService地址")]
        //public string WebServiceUrl
        //{
        //    get { return _webServiceUrl; }
        //    set
        //    {
        //        _webServiceUrl = value;
        //        this.DoNotify();
        //    }
        //}

        //private string _callMethodName;

        //[XmlElement("调用方法名")]
        //public string CallMethodName
        //{
        //    get { return _callMethodName; }
        //    set
        //    {
        //        _callMethodName = value;
        //        this.DoNotify();
        //    }
        //}

        //private string _station;

        //[XmlElement("站号")]
        //public string Station
        //{
        //    get { return _station; }
        //    set
        //    {
        //        _station = value;
        //        this.DoNotify();
        //    }
        //}

        //private bool _uploadMes;

        //[XmlElement("上传MES开关")]
        //public bool UploadMes
        //{
        //    get { return _uploadMes; }
        //    set
        //    {
        //        _uploadMes = value;
        //        this.DoNotify();
        //    }
        //}

        public int GetNewProjNo()
        {
            if (ListConfig is null)
            {
                return 0;
            }

            int minNo = ListConfig.Where(t => t.No >= 0).Select(t => t.No).OrderBy(n => n).FirstOrDefault();

            if (minNo == 0)
            {
                int maxNo = ListConfig.Max(t => t.No);
                return Enumerable.Range(minNo, maxNo + 2).Except(ListConfig.Select(t => t.No)).FirstOrDefault();
            }

            return 0;
        }

        private int _usingNo;

        [XmlAttribute("启用方案编号")]
        public int UsingNo
        {
            get { return _usingNo; }
            set
            {
                _usingNo = value;
                this.DoNotify();
            }
        }

        private ObservableCollection<PcbConfig> _listConfig = new ObservableCollection<PcbConfig>();

        [XmlElement("测试方案")]
        public ObservableCollection<PcbConfig> ListConfig
        {
            get { return _listConfig; }
            set
            {
                _listConfig = value;
                this.DoNotify();
            }
        }

        public AllPcbConfig()
        {
            ListConfig.CollectionChanged += ListConfig_CollectionChanged;
        }

        private void ListConfig_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            ListConfig = sender as ObservableCollection<PcbConfig>;
        }
    }

    /// <summary>
    /// 控制板配置
    /// </summary>
    [Serializable]
    public class PcbConfig : NotifyBase
    {
        private int _no;

        [XmlAttribute("编号")]
        public int No
        {
            get { return _no; }
            set
            {
                _no = value;
                this.DoNotify();
            }
        }

        private string _name;

        [XmlAttribute("名称")]
        public string Name
        {
            get { return _name; }
            set
            {
                _name = value;
                this.DoNotify();
            }
        }

        private string _visionProjPath;

        [XmlElement("视觉方案路径")]
        public string VisionProjPath
        {
            get { return _visionProjPath; }
            set
            {
                _visionProjPath = value;
                this.DoNotify();
            }
        }

        private int _probeUsedTimes;

        [XmlElement("探针使用次数")]
        public int ProbeUsedTimes
        {
            get { return _probeUsedTimes; }
            set
            {
                _probeUsedTimes = value;
                this.DoNotify();
            }
        }

        private int _probeMaxUsedTimes;

        [XmlElement("探针报警次数")]
        public int ProbeMaxUsedTimes
        {
            get { return _probeMaxUsedTimes; }
            set
            {
                _probeMaxUsedTimes = value;
                this.DoNotify();
            }
        }

        private ObservableCollection<SingBoardConfig> _listSingBoardConfig = new ObservableCollection<SingBoardConfig>();

        [XmlElement("功能测试配置")]
        public ObservableCollection<SingBoardConfig> ListSingBoardConfig
        {
            get { return _listSingBoardConfig; }
            set
            {
                _listSingBoardConfig = value;
                this.DoNotify();
            }
        }

        public PcbConfig()
        {
            ListSingBoardConfig.CollectionChanged += ListLedCurrentConfig_CollectionChanged;
        }

        private void ListLedCurrentConfig_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            ListSingBoardConfig = sender as ObservableCollection<SingBoardConfig>;
        }
    }

    /// <summary>
    /// 电流测试配置
    /// </summary>
    [Serializable]
    public class SingBoardConfig : NotifyBase
    {
        private ObservableCollection<PowerConfig> _listPowerConfig = new ObservableCollection<PowerConfig>();

        [XmlElement("电源配置")]
        public ObservableCollection<PowerConfig> ListPowerConfig
        {
            get { return _listPowerConfig; }
            set
            {
                _listPowerConfig = value;
                this.DoNotify();
            }
        }

        private ObservableCollection<DOConfig> _listDoConfig = new ObservableCollection<DOConfig>();

        [XmlElement("输出配置")]
        public ObservableCollection<DOConfig> ListDOConfig
        {
            get { return _listDoConfig; }
            set
            {
                _listDoConfig = value;
                this.DoNotify();
            }
        }

        private void ListPowerConfig_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            ListPowerConfig = sender as ObservableCollection<PowerConfig>;
        }

        public SingBoardConfig()
        {
            ListPowerConfig.CollectionChanged += ListPowerConfig_CollectionChanged;
            ListDOConfig.CollectionChanged += ListDOConfig_CollectionChanged;
        }

        private void ListDOConfig_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            ListDOConfig = sender as ObservableCollection<DOConfig>;
        }


        private int _stationNo;
        [XmlAttribute("工位编号")]
        public int StationNo
        {
            get { return _stationNo; }
            set
            {
                _stationNo = value;
                this.DoNotify();
            }
        }


        private string _ledDesc;

        [XmlAttribute("功能")]
        public string LedDesc
        {
            get { return _ledDesc; }
            set
            {
                _ledDesc = value;
                this.DoNotify();
            }
        }
        private string _pattern;
        [XmlAttribute("正则匹配")]
        public string Pattern
        {
            get { return _pattern; }
            set
            {
                _pattern = value;
                this.DoNotify();
            }
        }


        private double _upperLimitCurrent;

        [XmlElement("电流上限")]
        public double UpperLimitCurrent
        {
            get { return _upperLimitCurrent; }
            set
            {
                _upperLimitCurrent = value;
                this.DoNotify();
            }
        }

        private double _lowerLimitCurrent;

        [XmlElement("电流下限")]
        public double LowerLimitCurrent
        {
            get { return _lowerLimitCurrent; }
            set
            {
                _lowerLimitCurrent = value;
                this.DoNotify();
            }
        }

        private string _procName;

        [XmlElement("视觉流程名称")]
        public string ProcName
        {
            get { return _procName; }
            set
            {
                _procName = value;
                this.DoNotify();
            }
        }

        private uint _procId;

        [XmlElement("视觉流程ID")]
        public uint ProcID
        {
            get { return _procId; }
            set
            {
                _procId = value;
                this.DoNotify();
            }
        }

        private int _waitTime;

        [XmlElement("上电等待时间")]
        public int WaitTime
        {
            get { return _waitTime; }
            set
            {
                _waitTime = value;
                this.DoNotify();
            }
        }

        private string _testMethodName;

        [XmlElement("测试调用方法名称")]
        public string TestMethodName
        {
            get { return _testMethodName; }
            set
            {
                _testMethodName = value;
                this.DoNotify();
            }
        }

        private int _snLength;

        [XmlElement("SN码长度")]
        public int SNLength
        {
            get { return _snLength; }
            set
            {
                _snLength = value;
                this.DoNotify();
            }
        }
    }

    /// <summary>
    /// 电源配置
    /// </summary>
    [Serializable]
    public class PowerConfig : NotifyBase
    {
        private int _channelIndex;

        [XmlAttribute("电源通道索引")]
        public int ChannelIndex
        {
            get { return _channelIndex; }
            set
            {
                _channelIndex = value;
                this.DoNotify();
            }
        }

        private bool _isActive;

        [XmlAttribute("是否启用")]
        public bool IsActive
        {
            get { return _isActive; }
            set
            {
                _isActive = value;
                this.DoNotify();
            }
        }

        private double _voltage;

        [XmlAttribute("输出电压")]
        public double Voltage
        {
            get { return _voltage; }
            set
            {
                _voltage = value;
                this.DoNotify();
            }
        }

        private double _current;

        [XmlAttribute("输出电流")]
        public double Current
        {
            get { return _current; }
            set
            {
                _current = value;
                this.DoNotify();
            }
        }
    }
}